from django.shortcuts import render
from rest_framework.response import Response
from rest_framework.views import APIView
from utils.authentication import JWTAuthenticationss

from cart import models, cartser


# Create your views here.
class AddCart(APIView):
    authentication_classes = [JWTAuthenticationss]

    def post(self, request):
        user = request.data.get('user')
        course = request.data.get('course')
        info = models.CartModel.objects.filter(user=user, course=course).first()
        if info:
            return Response({"code": 200, "msg": "已存在！"})
        else:
            ser = cartser.CartSerializer(data=request.data)
            if ser.is_valid():
                ser.save()
                return Response({"code": 200, "msg": "添加成功！"})
            else:
                return Response({"code": 204, "msg": "添加失败！"})


class CartList(APIView):
    authentication_classes = [JWTAuthenticationss]

    def get(self, request):
        user = request.query_params.get('user')
        datalist = models.CartModel.objects.filter(user=user)
        print(datalist)
        ser = cartser.CartSerializer(datalist, many=True)
        cardata = []
        total = 0
        selectnum = 0
        for i in ser.data:
            if i["selected"]:
                selectnum += 1
                total += i["courselist"].get("price")
        return Response({"msg": "ok", "cartlist": ser.data, "total": total, "selectnum": selectnum})


class CartUpdate(APIView):
    authentication_classes = [JWTAuthenticationss]

    def put(self, request):
        id = request.data.get('id')
        cardata = models.CartModel.objects.filter(id=id).first()
        cardata.selected = not cardata.selected
        cardata.save()
        return Response({"msg": "ok", "data": "修改成功！"})


class CartDelete(APIView):
    authentication_classes = [JWTAuthenticationss]

    def delete(self, request):
        id = request.query_params.get('id')
        models.CartModel.objects.filter(id=id, selected=True).delete()
        return Response({"msg": "ok", "data": "删除成功！"})


class CartManyDelete(APIView):
    authentication_classes = [JWTAuthenticationss]

    def delete(self, request):
        id = request.query_params.get("user")
        models.CartModel.objects.filter(user=id, selected=True).delete()
        return Response({"msg": "ok", "data": "删除成功！"})


class CartAll(APIView):
    authentication_classes = [JWTAuthenticationss]

    def put(self, request):
        user = request.data.get('user')
        status = request.data.get('status')
        models.CartModel.objects.filter(user=user).update(selected=status)
        if status:
            return Response({"msg": "ok", "data": "全选成功！"})
        else:
            return Response({"msg": "ok", "data": "取消全选成功！"})


class CartSel(APIView):
    authentication_classes = [JWTAuthenticationss]

    def get(self, request):
        user = request.query_params.get('user')
        cartdata = models.CartModel.objects.filter(user=user, selected=True)
        ser = cartser.CartSerializer(cartdata, many=True)
        total = 0
        for i in ser.data:
            total += i["courselist"].get("price")
        return Response({"msg": "ok", "data": ser.data, "total": total})


